package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.AccessModeAttrHandler;
import com.iwhalecloud.bss.kite.cucc.service.util.CalculatePkgUsageUtil;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description: 接入方式
 * File Name:AccessModeAttrLinkHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-6-7 17:23   邱萌         1.0       1.0 Version
 **/
@Service
@AttrHandler(AccessModeAttrHandler.class)
public class AccessModeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //获取省份标识
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String serviceOfferId = data.getServiceOfferId();
        //福建个性需求
        if (LocalKeyConsts.PROVINCE_FUJIAN.equals(provinceCode)) {
            //接入方式
            if ("2_0000066".equals(attr.getFieldName())) {
                KiteAttr kiteAttr = inst.getAttr("200001206");
                String ywzlx = "";
                kiteAttr.setShowType("E");
                if ("100006847".equals(attr.getValue())) {//DIA
                    kiteAttr.setValue("");
                    kiteAttr.setValueDesc("");
                    ywzlx = "100006848";
                } else if ("100006595".equals(attr.getValue())) {//UTN
                    kiteAttr.setValue("");
                    kiteAttr.setValueDesc("");
                    ywzlx = "100006846";
                } else if ("100006849".equals(attr.getValue())) {//CNCNET
                    kiteAttr.setValue("");
                    kiteAttr.setValueDesc("");
                    ywzlx = "100006850";
                } else if (KiteStringUtils.equalsAny(attr.getValue(), "110007637", "1_0000360")) {
                    ywzlx = "100006841";
                    //联动上行速率展示且等于端口带宽
                    if(attr.getValue().equals("1_0000360")){
                        if(null==kiteAttr.getValue()||StringUtils.isBlank(kiteAttr.getValue())){
                            KiteAttr portAttr = inst.getAttr("2_0000061");
                            KiteAttrValue kiteAttrValue = kiteAttr.getAttrValues().stream().filter(attrValue -> attrValue.getAttrValue().equals(portAttr.getValue())).findFirst().orElse(null);
                            if(null!=kiteAttrValue&& StringUtils.isNotBlank(kiteAttrValue.getAttrValue())){
                                kiteAttr.setValue(kiteAttrValue.getAttrValue());
                                kiteAttr.setValueDesc(kiteAttrValue.getAttrValueDesc());
                            }
                        }
                        kiteAttr.setShowType(LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D);
                    }else{
                        kiteAttr.setValue("");
                        kiteAttr.setValueDesc("");
                    }

                }
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), kiteAttr);
                KiteAttr childAttr = inst.getAttr("200002072");
                childAttr.setValue(ywzlx);
                String finalYwzlx = ywzlx;
                childAttr.setValueDesc(childAttr.getAttrValues().stream().filter(attrValue -> attrValue.getAttrValue().equals(finalYwzlx)).findFirst().get().getAttrValueDesc());
                operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), childAttr);

            }
            if("200001206".equals(attr.getFieldName())){
                KiteAttr portAttr = inst.getAttr("2_0000061");
                String speed = attr.getValueDesc();
                String speedPort = portAttr.getValueDesc();
                if(!CalculatePkgUsageUtil.compareWidth(speedPort,speed)){
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("上行速率不能大于端口带宽，请重新选择商品");                }
            }
            //业务子类型
            if ("200002072".equals(attr.getFieldName())) {
                String jrfs = "";
                if ("100006848".equals(attr.getValue())) {//尊享专线
                    jrfs = "100006847";
                } else if ("100006846".equals(attr.getValue())) {//智享专线
                    jrfs = "100006595";
                } else if ("100006850".equals(attr.getValue())) {//国际精品网
                    jrfs = "100006849";
                } else if ("100006841".equals(attr.getValue())) {
                    jrfs = "110007637";
                }
                operateInstAttrService.setAttrValue(inst, "2_0000066", jrfs, data.getSceneInstId(), null);
            }
        } else if (LocalKeyConsts.PROVINCE_ZHEJIANG.equals(provinceCode)&& KiteStringUtils.equalsAny(serviceOfferId,"1","2826","1014")) {
            //浙江 接入方式为“PON接入（光纤到户）”时
            if ("2_0000066".equals(attr.getFieldName())) {
                if ("100005455".equals(attr.getValue())){
                    //展示是否新增设备
                    KiteAttr isNewEquipmentAttr = querySceneInstService.getCommonAttr(data.getSceneInstId(), "ORD10145");
                    if (Objects.nonNull(isNewEquipmentAttr)) {
                        isNewEquipmentAttr.setValue("0");
                        isNewEquipmentAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                        operateSceneCommonService.setCommonAttr(data.getSceneInstId(), isNewEquipmentAttr);
                    }
                    //用户地址是否需要广播 属性值默认为否，置灰
                    KiteAttr isBroadcastUserAddrAttr = inst.getAttr("2_0000087");
                    if (Objects.nonNull(isBroadcastUserAddrAttr)) {
                        isBroadcastUserAddrAttr.setIsEdit(KeyConsts.IFTRUE_F);
                        isBroadcastUserAddrAttr.setValue("1_0000107");
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), isBroadcastUserAddrAttr);
                    }
                    //上联机房 枚举值仅允许选择：【其他】、【省内骨干网】
                    KiteAttr uplinkRoomAttr = inst.getAttr("2_0000065");
                    if (Objects.nonNull(uplinkRoomAttr)) {
                        List<KiteAttrValue> attrValues = new ArrayList<>();
                        KiteAttrValue attrValue0 = new KiteAttrValue();
                        attrValue0.setAttrValue("");
                        attrValue0.setAttrValueName("--请选择--");
                        attrValue0.setSortBy("0");
                        attrValues.add(attrValue0);
                        Map<String, String> map = new HashMap();
                        map.put("1_0000151","其他");
                        map.put("1_0000350", "省内骨干网");
                        for (Map.Entry<String, String> entry : map.entrySet()) {
                            KiteAttrValue attrValue = new KiteAttrValue();
                            attrValue.setAttrValue(entry.getKey());
                            attrValue.setAttrId("20210313652");
                            attrValue.setAttrValueName(entry.getValue());
                            attrValues.add(attrValue);
                        }
                        uplinkRoomAttr.setAttrValues(attrValues);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), uplinkRoomAttr);
                    }
                }else {
                    //展示是否新增设备
                    KiteAttr isNewEquipmentAttr = querySceneInstService.getCommonAttr(data.getSceneInstId(), "ORD10145");
                    if (Objects.nonNull(isNewEquipmentAttr)) {
                        isNewEquipmentAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                        operateSceneCommonService.setCommonAttr(data.getSceneInstId(), isNewEquipmentAttr);
                    }
                    //用户地址是否需要广播 属性值放开置灰
                    KiteAttr isBroadcastUserAddrAttr = inst.getAttr("2_0000087");
                    if (Objects.nonNull(isBroadcastUserAddrAttr)) {
                        isBroadcastUserAddrAttr.setIsEdit(KeyConsts.IFTRUE_T);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), isBroadcastUserAddrAttr);
                    }
                    //上联机房
                    KiteAttr uplinkRoomAttr = inst.getAttr("2_0000065");
                    if (Objects.nonNull(uplinkRoomAttr)) {
                        List<KiteAttrValue> attrValues = new ArrayList<>();
                        KiteAttrValue attrValue0 = new KiteAttrValue();
                        attrValue0.setAttrValue("");
                        attrValue0.setAttrValueName("--请选择--");
                        attrValue0.setSortBy("0");
                        attrValues.add(attrValue0);
                        Map<String, String> map = new HashMap();
                        map.put("1_0000151","其他");
                        map.put("1_0000349", "全国骨干网");
                        map.put("1_0000350", "省内骨干网");
                        for (Map.Entry<String, String> entry : map.entrySet()) {
                            KiteAttrValue attrValue = new KiteAttrValue();
                            attrValue.setAttrValue(entry.getKey());
                            attrValue.setAttrId("20210313652");
                            attrValue.setAttrValueName(entry.getValue());
                            attrValues.add(attrValue);
                        }
                        uplinkRoomAttr.setAttrValues(attrValues);
                        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), uplinkRoomAttr);
                    }
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
